$(function(){
    "use strict";

    // 当点击直接投入的时候
    $('.directInto_box>p').click(function(){
        $(this).siblings('ul.directInto_list').toggleClass('active')
    })
    //当点击核算确定的时候
    $('#check').on('click','.cancel_btn',function(){
        $('#check').removeClass('active')
    })
    // 点击出现搜索框
    $('.hidden_box_showBox').click(function(){
        $(this).siblings('.hidden_box_hiddenBox').toggleClass('active')
        //进入时，直接请求搜索框中需要的数据
        if($(this).children('i').hasClass('icon-jiantou')){
            $(this).children('i').removeClass('icon-jiantou').addClass('icon-jiantou-top')
            let that = this
            $.ajax({//部门
                url:'/Admin/Org/showDep',
                type:'get',
                success:function(data){
                    let showDepHTML = '<option></option>'
                    for(var sdi=0;sdi<data.length;sdi++){
                        showDepHTML += `
                    <option value="${data[sdi].id}">${data[sdi].dep_name}</option>
                `
                    }
                    $(that).siblings('.hidden_box_hiddenBox').find('[data-query="dep"]').html(showDepHTML)
                }
            })
            $.ajax({//人
                url:'/Admin/Pay/getStaffName',
                type:'get',
                success:function(data){
                    let peoplehtmls = '<option></option>'
                    for(var pi=0;pi<data.length;pi++){
                        peoplehtmls += `
                            <option value="${data[pi].id}" data-dep="${data[pi].dep_name}">${data[pi].staff_name}</option>
                        `
                    }
                    $(that).siblings('.hidden_box_hiddenBox').find('[data-query="name"]').html(peoplehtmls)
                }
            })
            $.ajax({//设备
                url:'/Admin/ProDevice/getDevName',
                type:'get',
                // xhrFields: { withCredentials: true }, crossDomain: true,
                success:function(data){
                    let eqhtml = '<option></option>'
                    for(var eqi=0;eqi<data.length;eqi++){
                        eqhtml += `
                            <option value="${data[eqi].id}" data-eq="${data[eqi].dev_sn}">${data[eqi].dev_name}</option>
                        `
                    }
                    $(that).siblings('.hidden_box_hiddenBox').find('[data-query="eq"]').html(eqhtml)
                }
            })
        }else{
            $(this).children('i').removeClass('icon-jiantou-top').addClass('icon-jiantou')
        }
    })
    // 当确定搜索时
    $('.hiddenBox_list_btn').on('click','button',function(){
        $('.hidden_box_hiddenBox').removeClass('active')
        $('.hidden_box_showBox').children('i').removeClass('icon-jiantou-top').addClass('icon-jiantou')
        if($(this).html() == '确定'){
            $('#dateVal').html(window.query_year_people||'全部')

            let time = $(this).closest('.hidden_box_hiddenBox').find('[data-query="time"]').val()
            let eq = $(this).closest('.hidden_box_hiddenBox').find('[data-query="eq"]').val()
            let name = $(this).closest('.hidden_box_hiddenBox').find('[data-query="name"]').val()
            let dep = $(this).closest('.hidden_box_hiddenBox').find('[data-query="dep"]').val()
            window.location.href = `/Admin/ProDevice/index?dep_id=${dep}&dev_name=${eq}&staff_name=${name}&time=${time}`
        }else{
            window.query_year_people = null
        }
    })
    //当点击查看记录时
    $('#table_table').on('click','.look_record_btn',function(e){
        e.preventDefault()
        $(this).siblings('.tip_hiddenBox').addClass('active')
    })
    // 当点击工时记录中确认按钮时
    $('#table_table').on('click','.work_record_box>.btn',function(e){
        e.preventDefault()
        $(this).parent().removeClass('active')
    })
    //全选的功能
    $('#checkAll').on('click',function(){
        let checkboxs =$('input[type=checkbox]:not("#checkAll")');
        for(let i=0;i<checkboxs.length;i++){
            let checkbox=checkboxs[i];
            if(checkbox.disabled){
                // colnsole.log(11)
                continue
            }else{
                checkbox.checked=this.checked;
            }
        }
    })
    $('#table_table').on('click','input[type=checkbox]:not("#checkAll")',function(){
        "use strict";
        var isCheckedAll = true;
        var checkboxs =$('input[type=checkbox]:not("#checkAll")');
        for (var i = 0; i < checkboxs.length; i++) {
            if (!checkboxs[i].checked) {
                isCheckedAll = false;
                break;
            }
        }
        $('#checkAll')[0].checked = isCheckedAll;
    })

    /*当点击添加按钮时*/
    $('.operation_btn').on('click','a',function(e){
        e.preventDefault()
        if( $(this).html() == '添加' ){
            var add_people = $(this).attr('href')
            $(`[lll=${add_people}]`).addClass('active')
            let peoplehtmls = '<option></option>';
            let eqhtml = '<option></option>'
            $.ajax({
                url:'/Admin/Pay/getStaffName',
                type:'get',
                success:function(data){
                    for(var i=0;i<data.length;i++){
                        peoplehtmls += `
                            <option value="${data[i].id}" data-dep="${data[i].dep_name}">${data[i].staff_name}</option>
                        `
                    }
                    $(`[lll=${add_people}]`).find('[add_name="add_name"]').html(peoplehtmls)
                }
            })
            $.ajax({
                url:'/Admin/ProDevice/getDevName',
                type:'get',
                success:function(data){
                    for(var i=0;i<data.length;i++){
                        eqhtml += `
                            <option value="${data[i].id}" data-eq="${data[i].dev_sn}">${data[i].dev_name}</option>
                        `
                    }
                    $(`[lll=${add_people}]`).find('[add_name="add_equipmentName"]').html(eqhtml)
                }
            })
        }
        if( $(this).html() == '核算' ){
            var add_people = $(this).attr('href')
            var checkboxs =$('#table_table input[type=checkbox]:not("#checkAll")');
            var len = 0;
            let hs_arr = [];
            for (var i = 0; i < checkboxs.length; i++) {
                if (checkboxs[i].checked) {
                    continue
                }else{
                    len++
                }
            }
            if(len == checkboxs.length){
                $('#yz_modal').html('您还没有选择记录！')
                $('#yz_modal').addClass('active')
                tipShow()
                return
            }else{
                for (let hsi = 0; hsi < checkboxs.length; hsi++) {
                    if (checkboxs[hsi].checked) {
                        hs_arr.push( $(checkboxs[hsi]).closest('tr[data-index]').attr('data-index') )
                    }else{
                        continue;
                    }
                }
                $.ajax({
                    url:'/Admin/ProDevice/calCulDevice',
                    type:'get',
                    data:{
                        id:hs_arr
                    },
                    success:function(data){
                        if(data.code==1){
                            window.location.href='/Admin/ProDevice/index'
                        }else{
                            $('#check').find('.check_success_num').html(data.success)
                            $('#check').find('.check_error_num').html(data.fail)
                            $('#check').find('.check_error_Reason').html(data.msg)
                            $(add_people).addClass('active')
                        }
                    }
                })
            }


        }
        if( $(this).html() == '批量删除' ){
            var checkboxs =$('input[type=checkbox]:not("#checkAll")');
            var len = 0;
            for (var i = 0; i < checkboxs.length; i++) {
                if (checkboxs[i].checked) {
                    continue
                }else{
                    len++
                }
            }
            if(len == checkboxs.length){
                $('#yz_modal').html('您还没有选择人员！')
                $('#yz_modal').addClass('active')
                tipShow()
                return
            }else{
                let _delete = $(this).attr('href');
                $(_delete).addClass('active')
            }

        }
        if( $(this).html() == '批量导入' ){
            let showbox_id = $(this).attr('href');
            $(showbox_id).addClass('active')
        }
    })
    //人员选择之后自动显示人员所在部门
    $(`[lll='add_people']`).find('[add_name="add_name"]').change(function(){
        let dep = $(this).children('option:selected').attr('data-dep')
        $(`[lll='add_people']`).find('[add_name="add_department"]').val(dep)
    })
    //设备选择之后自动显示设备编号
    $(`[lll='add_people']`).find('[add_name="add_equipmentName"]').change(function(){
        let eq = $(this).children('option:selected').attr('data-eq')
        $(`[lll='add_people']`).find('[add_name="add_equipmentNum"]').val(eq)
    })

    // 批量删除
    $('.delete_tip_btn_box').on('click','button',function(){
        "use strict";
        let delete_arr = [];
        if($(this).html() == '确认'){
            let checkboxs =$('#table_table input[type=checkbox]:not("#checkAll")');
            for (let i = 0; i < checkboxs.length; i++) {
                if (checkboxs[i].checked) {
                    delete_arr.push( $(checkboxs[i]).closest('tr[data-index]').attr('data-index') )
                }else{
                    continue;
                }
            }
            $.ajax({
                url:'/Admin/ProDevice/multiDelDevice',
                type:'post',
                data:{
                    pro_dev_id: delete_arr
                },
                success:function(data){
                    if(data.code==1){
                        for(let r = 0; r < delete_arr.length; r++){
                            $('#table_table').children('tbody').children(`[data-index=${delete_arr[r]}]`).remove();
                        }
                        $('#delete').removeClass('active')
                        window.location.href = `/Admin/ProDevice/index`//强制刷新
                    }else{
                        $('#yz_modal').html(data.msg)
                        $('#yz_modal').addClass('active')
                        tipShow()
                        return
                    }
                    checkBox()
                }
            })

        }else{
            $('#delete').removeClass('active')

        }
    })

    // 当点击编辑按钮时
    $('#table_table').on('click','.edit_table',function(e){
        e.preventDefault()
        var edit_box = $(this).attr('href')
        if($(this).html()=='编辑') {
            let edit_id = $(this).closest('tr').attr('data-index')
            let edit_peopleHTML = ''
            let edit_eqHTML = ''
            $.ajax({
                url: '/Admin/Pay/getStaffName',
                type: 'get',
                success: function (data) {
                    for (var epi = 0; epi < data.length; epi++) {
                        edit_peopleHTML += `
                            <option value="${data[epi].id}" data-dep="${data[epi].dep_name}">${data[epi].staff_name}</option>
                        `
                    }
                    $(`[lll=${edit_box}]`).find('[add_name="edit_name"]').html(edit_peopleHTML)
                }
            })
            $.ajax({
                url: '/Admin/ProDevice/getDevName',
                type: 'get',
                success: function (data) {
                    // console.log(data)
                    for (var eei = 0; eei < data.length; eei++) {
                        edit_eqHTML += `
                            <option value="${data[eei].id}" data-eq="${data[eei].dev_sn}">${data[eei].dev_name}</option>
                        `
                    }
                    $(`[lll=${edit_box}]`).find('[add_name="edit_equipmentName"]').html(edit_eqHTML)
                }
            })
            $.ajax({
                url:'/Admin/ProDevice/editDevice',
                type:'get',
                data:{id:edit_id},
                success:function(data){
                    if(data.code==1){
                        let datas = data.data
                        $(`[lll=${edit_box}]`).find('[add_name=start_time_edit]').val(datas.start_time)

                        let max_days = datas.max_days
                        let dayListHtmls = ''
                        for(let i=1; i<=max_days; i++){
                            dayListHtmls+=`<li>${i}</li>`
                        }
                        dayListHtmls+=`
                            <div class='chooseDayBtn_box'>
                                <div>
                                    <button class='chooseDayBtn'>清空</button>
                                    <button class='chooseDayBtn'>确认</button>
                                </div>
                            </div>
                        `
                        $('#editList').attr('data-hours',datas.standard_hours)
                        $('#editList').html(dayListHtmls)

                        $(`[lll=${edit_box}]`).find('[add_name=edit_equipmentName]').val(datas.dev_id)
                        $(`[lll=${edit_box}]`).find('[add_name=edit_equipmentNum]').val(datas.dev_sn)

                        $(`[lll=${edit_box}]`).find(`[add_name='edit_name']`).val(datas.staff_id)
                        $(`[lll=${edit_box}]`).find(`[add_name='edit_department']`).val(datas.dep_name)
                        $(`[lll=${edit_box}]`).find(`[add_name='edit_workTime']`).val(datas.work_hours)//工时

                        // $(`[lll=${edit_box}]`).find('[add_name=end_time_edit]').val(datas.end_time)
                        
                        // $(`[lll=${edit_box}]`).find('[add_name=edit_name]').val(datas.staff_id)
                        // $(`[lll=${edit_box}]`).find('[add_name=edit_department]').val(datas.dep_name)
                        // $(`[lll=${edit_box}]`).find('[add_name=edit_workTime]').val(datas.work_hours)
                        // $(`[lll=${edit_box}]`).find('[add_name=edit_equipmentUseRecord]').val(datas.work_record)

                        let work_day = datas.work_day
                        let html = ''
                        let works_daysList = $('#editList').children('li')
                        for(let i=0;i<work_day.length;i++){
                            html += `
                                <li>
                                    <span data-day='${work_day[i].day}'>${work_day[i].date}:</span>
                                    <input type="text" value="${work_day[i].work_hours}" class="input_work_hours BFBFBF_inputStyle add_people_input" style='width:40px !important;margin-right:10px;text-align:center;padding-left:0' placeholder='工时'>
                                    <input type="text" class="BFBFBF_inputStyle add_people_input" placeholder='请输入工时记录' value='${work_day[i].record}'>
                                    <i class='iconfont icon-cha'></i>
                                </li>
                            `
                            for(let j=0; j<works_daysList.length; j++){
                                if(work_day[i].day==$(works_daysList[j]).html()){
                                    $(works_daysList[j]).addClass('active')
                                }
                            }
                        }
                        $('#edit_workTime_list').html(html)

                        window.sessionStorage['edit_id']=edit_id
                        $(`[lll=${edit_box}]`).addClass('active')
                    }else{

                    }
                }
            })
        }else{
            let cancel_hesuan_id = $(this).closest('tr[data-index]').attr('data-index')
            let that = this
            $.ajax({
                url: '/Admin/ProDevice/canelDev',
                type: 'get',
                data: {
                    id: cancel_hesuan_id
                },
                success: function(data){
                    if(data.code==1){
                        // console.log(1)
                        $(that).closest('tr[data-index]').find('input').removeAttr('disabled')
                        $(that).html('编辑')
                        $(that).attr('href','edit_people')
                    }
                }
            })
        }
    })
    //人员选择之后自动显示人员所在部门
    $(`[lll='edit_people']`).find('[add_name="edit_name"]').change(function(){
        let dep = $(this).children('option:selected').attr('data-dep')
        $(`[lll='edit_people']`).find('[add_name="edit_department"]').val(dep)
    })
    //设备选择之后自动显示设备编号
    $(`[lll='edit_people']`).find('[add_name="edit_equipmentName"]').change(function(){
        let eq = $(this).children('option:selected').attr('data-eq')
        $(`[lll='edit_people']`).find('[add_name="edit_equipmentNum"]').val(eq)
    })

// 保存按钮点击时
    $('#save_btn').click(function(){
        let start_time_edit = $(this).closest('[lll]').find('[add_name=start_time_edit]').val()
        // let end_time_edit = $(this).closest('[lll]').find('[add_name=end_time_edit]').val()

        let edit_equipmentName_id = $(this).closest('[lll]').find('[add_name=edit_equipmentName]').val()
        let edit_equipmentName = $(this).closest('[lll]').find('[add_name=edit_equipmentName] option:selected').text()
        let edit_equipmentNum = $(this).closest('[lll]').find('[add_name=edit_equipmentNum]').val()
        let edit_name_id = $(this).closest('[lll]').find('[add_name=edit_name]').val()
        let edit_name = $(this).closest('[lll]').find('[add_name=edit_name] option:selected').text()
        let edit_department = $(this).closest('[lll]').find('[add_name=edit_department]').val()
        let edit_workTime = $(this).closest('[lll]').find('[add_name=edit_workTime]').val()
        let edit_equipmentUseRecord = $(this).closest('[lll]').find('[add_name=edit_equipmentUseRecord]').val()/**/

        // var start_timeList = start_time_edit.split('-')
        // var end_timeList = end_time_edit.split('-')
        // var start_time_edit_month = start_timeList[1]
        // var end_time_edit_month = end_timeList[1]
        // var start_time_edit_date = start_timeList[2]
        // var end_time_edit_date = end_timeList[2]

        let edit_id = window.sessionStorage['edit_id']
        let that = this
        // if(start_time_edit_month != end_time_edit_month){
        //     $('#yz_modal').html('开始和结束时间不能跨月！')
        //     $('#yz_modal').addClass('active')
        //     tipShow()
        //     return
        // }else if(end_time_edit_date <= start_time_edit_date){
        //     $('#yz_modal').html('结束日期不得小于或等于开始日期！')
        //     $('#yz_modal').addClass('active')
        //     tipShow()
        //     return
        // }
        let work_list = $('#edit_workTime_list').children('li')
        let workTime_arr = []
        for(let i=0;i<work_list.length;i++){
            // console.log($(work_list[i]).children('span').attr('data-day'))
            let day = $(work_list[i]).children('span').attr('data-day')
            let hours = $(work_list[i]).children('input:nth-child(2)').val()
            let record = $(work_list[i]).children('input:nth-child(3)').val()
            let arr2 = []
            arr2.push(day)
            arr2.push(hours)
            arr2.push(record)
            workTime_arr.push(arr2)
        }

        if(start_time_edit == '' || edit_equipmentName == '' || edit_name == '' || edit_department == '' || edit_workTime == ''){
            $('#yz_modal').html('您有内容没有填写完整！')
            $('#yz_modal').addClass('active')
            tipShow()
            return
        }
        //防止重复点击按钮
            if( $(this).attr('lock') ) return
            $(this).attr('lock','lock')

        // let that = this
        $.ajax({
            url:'/Admin/ProDevice/editDevice',
            type:'post',
            data:{
                id:edit_id,
                dev_id: edit_equipmentName_id,
                staff_id: edit_name_id,
                start_time: start_time_edit,
                // end_time: end_time_edit,
                works:workTime_arr,
                work_hours: edit_workTime
                // work_record: edit_equipmentUseRecord
            },
            success:function(data){
                if(data.code==1){
                    let OldHTML = $('#table_table').find(`tr[data-index=${edit_id}]`)
                    let NewHTML = `
                            <td><input type="checkbox" class="checkboxStyle"></td>
                            <td data-name="start_time_edit">${start_time_edit}</td>
                            <td data-name="edit_equipmentName">${edit_equipmentName}</td>
                            <td data-name="edit_equipmentNum">${edit_equipmentNum}</td>
                            <td data-name="edit_name">${edit_name}</td>
                            <td data-name="edit_department">${edit_department}</td>
                            <td data-name="edit_workTime">${edit_workTime}</td>
                            <td class="look_record" data-name="edit_workTimeRecord">
                                <a href="#" class="look_record_btn">查看记录</a>
                                
                                <div class="tip_hiddenBox work_record_box">
                                    <span class="work_record_title">工时记录</span>
                                    `
                                    let date = data.work_day
                                    NewHTML += '<ul>'
                                    for(let i=0;i<date.length;i++){
                                        NewHTML += `
                                            <li>${date[i].date}:${date[i].record}</li>
                                        `
                                    }
                                    NewHTML += '</ul>'

                        NewHTML +=`
                                    <button class="btn btn-sm btn-success">确认</button>
                                </div>
                            </td>
                            <td style><a href="edit_people" class="edit_table">编辑</a></td>
                        `
                    OldHTML.html(NewHTML)
                    $(that).parent().parent().removeClass('active')
                }else{

                }
                $(that).removeAttr('lock')
            }
        })

    })


    /*选择日期*/
    let chooseDayListArr = []
    $('.chooseDayList').on('click','li',function(){
        let index = $(this).index()+1
        let list = $(this).closest('ul.chooseDayList').children()

        if($(this).hasClass('active')){
            $(this).removeClass('active')
            chooseDayListArr.splice(chooseDayListArr.indexOf(index),1);
        }else{

            if(chooseDayListArr.length<1){
                $(this).addClass('active')
                chooseDayListArr.push(index)
            }else{
                for(let r of chooseDayListArr){
                    for(let i=0;i<list.length;i++){
                        if($(list[i]).html()==r){
                            if(r<index){
                                for(let k = r; k<index;k++){
                                    $(list[k]).addClass('active')
                                    chooseDayListArr=[]
                                }
                            }else{
                                for(let k = index-1; k<r;k++){
                                    $(list[k]).addClass('active')
                                    chooseDayListArr=[]
                                }
                            }
                        }
                    }
                }
            }
        }

    })

    $('.chooseDayList').on('click','.chooseDayBtn',function(e){
        e.preventDefault()
        let list = $(this).closest('ul.chooseDayList').children()
        if($(this).html()=='确认'){
            let date = $(this).closest('ul[lll]').children('li:first-child').find('input').val()
            let arr = []
            let htmls = ''
            let hours = $(this).closest('ul.chooseDayList').attr('data-hours')

            for(let i=0; i<list.length; i++){
                if($(list[i]).hasClass('active')){
                    arr.push($(list[i]).html())
                    // let hours = $(list[i]).attr('data-hours')
                    htmls+=`
                        <li>
                            <span data-day='${$(list[i]).html()}'>${date}-${$(list[i]).html()}:</span>
                            <input type="text" autocomplete="off" value="${hours}" class="input_work_hours BFBFBF_inputStyle add_people_input" style='width:40px !important;margin-right:10px;text-align:center;padding-left:0' placeholder='工时'>
                            <input type="text" autocomplete="off" class="BFBFBF_inputStyle add_people_input" placeholder='请输入工时记录'>
                            <i class='iconfont icon-cha'></i>
                         </li>
                    `
                }
            }
            $(this).closest('ul[lll]').find('ul.workTime_list').html(htmls)
            
            let work_hours = arr.length*=hours
            $(this).closest('ul[lll]').find('ul.workTime_list').prev().find('input').val(work_hours)
        }else if($(this).html()=='清空'){
            for(let i=0; i<list.length; i++){
                if($(list[i]).hasClass('active')){
                    $(list[i]).removeClass('active')
                }
            }
            $(this).closest('ul[lll]').find('ul.workTime_list').html('')
            chooseDayListArr = []
        }

    })

    /*监听所有的工时框的变化*/
    $('.workTime_list').on('blur','.input_work_hours',function(){
        let list = $(this).closest('ul[lll]').find('.workTime_list').children()
        let hours = 0
        for(let i=0; i<list.length; i++){
            hours+=Number($(list[i]).find('.input_work_hours').val())
        }
        $(this).closest('ul[lll]').find('ul.workTime_list').prev().find('input').val(hours)
    })


    // 确认按钮点击时
    $('#add_btn').click(function(){
        let start_time_add = $(this).closest('[lll]').find('[add_name=start_time_add]').val()
        console.log(start_time_add)
        // let end_time_add = $(this).closest('[lll]').find('[add_name=end_time_add]').val()
        let add_equipment_id = $(this).closest('[lll]').find('[add_name=add_equipmentName]').val()
        let add_equipmentName = $(this).closest('[lll]').find('[add_name=add_equipmentName] option:selected').text()
        let add_equipmentNum = $(this).closest('[lll]').find('[add_name=add_equipmentNum]').val()
        let add_name_id = $(this).closest('[lll]').find('[add_name=add_name]').val()
        let add_name = $(this).closest('[lll]').find('[add_name=add_name] option:selected').text()
        let add_department = $(this).closest('[lll]').find('[add_name=add_department]').val()
        let add_workTime = $(this).closest('[lll]').find('[add_name=add_workTime]').val()
        // let add_equipmentUseRecord = $(this).closest('[lll]').find('[add_name=add_equipmentUseRecord]').val()/**/

        // var start_timeList = start_time_add.split('-')
        // var end_time_addList = end_time_add.split('-')
        // var start_time_add_month = start_timeList[1]
        // var end_time_add_month = end_time_addList[1]
        // var start_time_add_date = start_timeList[2]
        // var end_time_add_date = end_time_addList[2]

        // if(start_time_add_month != end_time_add_month){
        //     $('#yz_modal').html('开始和结束时间不能跨月！')
        //     $('#yz_modal').addClass('active')
        //     tipShow()
        //     return
        // }else if(end_time_add_date <= start_time_add_date){
        //     $('#yz_modal').html('结束日期不得小于或等于开始日期！')
        //     $('#yz_modal').addClass('active')
        //     tipShow()
        //     return
        // }
        let work_list = $('#add_workTime_list').children('li')
        let workTime_arr = []
        for(let i=0;i<work_list.length;i++){
            // console.log($(work_list[i]).children('span').attr('data-day'))
            let day = $(work_list[i]).children('span').attr('data-day')
            let hours = $(work_list[i]).children('input:nth-child(2)').val()
            let record = $(work_list[i]).children('input:nth-child(3)').val()
            let arr2 = []
            arr2.push(day)
            arr2.push(hours)
            arr2.push(record)
            workTime_arr.push(arr2)
        }
        // if(!add_equipmentUseRecord){
        //     add_equipmentUseRecord = '暂无'
        // }
        if(start_time_add == '' || add_equipmentName == '' || add_name == '' || add_department == '' || add_workTime == ''){
            $('#yz_modal').html('您有内容没有填写完整！')
            $('#yz_modal').addClass('active')
            tipShow()
            return
        }
        //防止重复点击按钮
        if( $(this).attr('lock') ) return
        $(this).attr('lock','lock')

        let that = this
        $.ajax({
            url:'/Admin/ProDevice/addDevice',
            type:'post',
            data:{
                dev_id: add_equipment_id,
                staff_id: add_name_id,
                start_time: start_time_add,
                // end_time: end_time_add,
                works:workTime_arr,
                work_hours: add_workTime
                // work_record: add_equipmentUseRecord
            },
            success:function(data){
                if(data.code==1){
                    // var peopleTableHTML = $('#table_table').children('tbody').html()
                    let peopleTableHTML = `
                    <tr data-index="${data.id}">
                            <td><input type="checkbox" class="checkboxStyle"></td>
                            <td data-name="start_time_edit">${start_time_add}</td>
                            <td data-name="edit_equipmentName">${add_equipmentName}</td>
                            <td data-name="edit_equipmentNum">${add_equipmentNum}</td>
                            <td data-name="edit_name">${add_name}</td>
                            <td data-name="edit_department">${add_department}</td>
                            <td data-name="edit_workTime">${add_workTime}</td>
                            <td class="look_record" data-name="edit_workTimeRecord">
                                <a href="#" class="look_record_btn">查看记录</a>
                                <div class="tip_hiddenBox work_record_box">
                                    <span class="work_record_title">工时记录</span>
                                    `
                                    let date = data.data
                                    peopleTableHTML += '<ul>'
                                    for(let i=0;i<date.length;i++){
                                        peopleTableHTML += `
                                            <li>${date[i].date}:${date[i].record}</li>
                                        `
                                    }
                                    peopleTableHTML += '</ul>'

                 peopleTableHTML +=`
                                    <button class="btn btn-sm btn-success">确认</button>
                                </div>
                            </td> 
                            <td style><a href="edit_people" class="edit_table">编辑</a></td>
                        </tr>
            `
                    $('#table_table').children('tbody').prepend(peopleTableHTML)

                    // $(that).closest('[lll]').find('[add_name]').val('')
                    $(that).closest('[lll]').find('[add_name=add_equipmentName]').val('')
                    $(that).closest('[lll]').find('[add_name=add_equipmentNum]').val('')
                    $(that).closest('[lll]').find('[add_name=add_name]').val('')
                    $(that).closest('[lll]').find('[add_name=add_department]').val('')

                    
                    $(that).closest('[lll]').removeClass('active')
                }else{
                    $('#yz_modal').html(data.msg)
                    $('#yz_modal').addClass('active')
                    tipShow()
                }
                $(that).removeAttr('lock')
            }
        })
    })
    // 确认并新增按钮点击时
    $('#add_AddNew_btn').click(function(){
        let start_time_add = $(this).closest('[lll]').find('[add_name=start_time_add]').val()
        // console.log(start_time_add)
        // let end_time_add = $(this).closest('[lll]').find('[add_name=end_time_add]').val()
        let add_equipment_id = $(this).closest('[lll]').find('[add_name=add_equipmentName]').val()
        let add_equipmentName = $(this).closest('[lll]').find('[add_name=add_equipmentName] option:selected').text()
        let add_equipmentNum = $(this).closest('[lll]').find('[add_name=add_equipmentNum]').val()
        let add_name_id = $(this).closest('[lll]').find('[add_name=add_name]').val()
        let add_name = $(this).closest('[lll]').find('[add_name=add_name] option:selected').text()
        let add_department = $(this).closest('[lll]').find('[add_name=add_department]').val()
        let add_workTime = $(this).closest('[lll]').find('[add_name=add_workTime]').val()
        // let add_equipmentUseRecord = $(this).closest('[lll]').find('[add_name=add_equipmentUseRecord]').val()/**/

        // var start_timeList = start_time_add.split('-')
        // var end_time_addList = end_time_add.split('-')
        // var start_time_add_month = start_timeList[1]
        // var end_time_add_month = end_time_addList[1]
        // var start_time_add_date = start_timeList[2]
        // var end_time_add_date = end_time_addList[2]

        // if(start_time_add_month != end_time_add_month){
        //     $('#yz_modal').html('开始和结束时间不能跨月！')
        //     $('#yz_modal').addClass('active')
        //     tipShow()
        //     return
        // }else if(end_time_add_date <= start_time_add_date){
        //     $('#yz_modal').html('结束日期不得小于或等于开始日期！')
        //     $('#yz_modal').addClass('active')
        //     tipShow()
        //     return
        // }
        let work_list = $('#add_workTime_list').children('li')
        let workTime_arr = []
        for(let i=0;i<work_list.length;i++){
            // console.log($(work_list[i]).children('span').attr('data-day'))
            let day = $(work_list[i]).children('span').attr('data-day')
            let hours = $(work_list[i]).children('input:nth-child(2)').val()
            let record = $(work_list[i]).children('input:nth-child(3)').val()
            let arr2 = []
            arr2.push(day)
            arr2.push(hours)
            arr2.push(record)
            workTime_arr.push(arr2)
        }
        // if(!add_equipmentUseRecord){
        //     add_equipmentUseRecord = '暂无'
        // }
        if(start_time_add == '' || add_equipmentName == '' || add_name == '' || add_department == '' || add_workTime == ''){
            $('#yz_modal').html('您有内容没有填写完整！')
            $('#yz_modal').addClass('active')
            tipShow()
            return
        }
        //防止重复点击按钮
        if( $(this).attr('lock') ) return
        $(this).attr('lock','lock')

        let that = this
        $.ajax({
            url:'/Admin/ProDevice/addDevice',
            type:'post',
            data:{
                dev_id: add_equipment_id,
                staff_id: add_name_id,
                start_time: start_time_add,
                // end_time: end_time_add,
                works:workTime_arr,
                work_hours: add_workTime
                // work_record: add_equipmentUseRecord
            },
            success:function(data){
                if(data.code==1){
                    // var peopleTableHTML = $('#table_table').children('tbody').html()
                    let peopleTableHTML = `
                    <tr data-index="${data.id}">
                            <td><input type="checkbox" class="checkboxStyle"></td>
                            <td data-name="start_time_edit">${start_time_add}</td>
                            <td data-name="edit_equipmentName">${add_equipmentName}</td>
                            <td data-name="edit_equipmentNum">${add_equipmentNum}</td>
                            <td data-name="edit_name">${add_name}</td>
                            <td data-name="edit_department">${add_department}</td>
                            <td data-name="edit_workTime">${add_workTime}</td>
                            <td class="look_record" data-name="edit_workTimeRecord">
                                <a href="#" class="look_record_btn">查看记录</a>
                                <div class="tip_hiddenBox work_record_box">
                                    <span class="work_record_title">工时记录</span>
                                    `
                                    let date = data.data
                                    peopleTableHTML += '<ul>'
                                    for(let i=0;i<date.length;i++){
                                        peopleTableHTML += `
                                            <li>${date[i].date}:${date[i].record}</li>
                                        `
                                    }
                                    peopleTableHTML += '</ul>'

                 peopleTableHTML +=`
                                    <button class="btn btn-sm btn-success">确认</button>
                                </div>
                            </td> 
                            <td style><a href="edit_people" class="edit_table">编辑</a></td>
                        </tr>
            `
                    $('#table_table').children('tbody').prepend(peopleTableHTML)

                    $(that).closest('[lll]').find('[add_name]').val('')
                    // $(that).closest('[lll]').removeClass('active')
                }else{
                    $('#yz_modal').html(data.msg)
                    $('#yz_modal').addClass('active')
                    tipShow()
                }
                $(that).removeAttr('lock')
            }
        })
    })

    // 取消按钮点击时
    $('.button_list').on('click','button',function(){
        if($(this).hasClass('cancel_btn')){
            $(this).closest('[lll]').removeClass('active')
        }
    })

    let msg = '文件正在上传，请勿重复点击！'

    // 上传文件
    $('#file_upload').change(function (e) {
        $(this).siblings('.import_tip_btn_box').find('button').removeClass('btn-grey').addClass('btn-success')
        msg = '文件正在上传，请勿重复点击！'
        $(this).siblings('.import_tip_btn_box').find('button').removeAttr('lock')
    })
    $('#import').find('button').click(function(e){
        "use strict";
        e.preventDefault()
        if($(this).hasClass('btn-success')){
            if($(this).html()=='确定导入'){
                //防止重复点击按钮
                if( $(this).attr('lock') ){
                    $('#yz_modal').html(msg)
                    $('#yz_modal').addClass('active')
                    tipShow()
                    return
                }
                $(this).attr('lock','lock')
                let that = this

                var formData = new FormData($("#file_form")[0]);
                $.ajax({
                    async : false,
                    cache : false,
                    type : "post",
                    data : formData,
                    url : '/Admin/ProDevice/importDevice',
                    dataType : 'json',
                    contentType: false, //必须
                    processData: false, //必须
                    success : function(data) {
                        if(data.code==1){
                            $('#import').removeClass('active')
                            window.location.href = `/Admin/ProDevice/index`//强制刷新
                            $(that).removeAttr('lock')
                        }else{
                            msg = data.msg
                            $('#yz_modal').html(msg)
                            $('#yz_modal').addClass('active')
                            tipShow()
                        }
                    }
                })
            }
        }

    })

    // 弹出来的小框框
    function tipShow(){
        window.tipBox = setTimeout(function(){
            // $('#yz_modal').removeClass('active')
            // $('#yz_modal').addClass('fadeOut')
            $('#yz_modal').removeClass('active')
            clearTimeout(tipBox)
        },2000)
    }
//关闭所有已选择的框框
    function checkBox(){
        var checkboxs =$('input[type=checkbox]');
        for (var i = 0; i < checkboxs.length; i++) {
            if (!checkboxs[i].checked) {
                continue;
            }else{
                $(checkboxs[i]).attr('checked',false)
            }
        }
    }

    //当点击区域不在导入弹框中时则关闭弹框
    $(document).click(function(e){
        let $this = $(e.target);
        if($this.closest('#import').length==0){
            if($this.attr('href') != '#import' && $this.children().attr('href') != '#import'){
                $('#import').removeClass('active');
            }
        }
    })

    //拖动拖动拖动
        let add_box =document.getElementById("add_people");
        let edit_box =document.getElementById("edit_people");
        add_box.onmousedown = function(ev){
            if($(ev.toElement).attr('id')=='add_people'){
                ev =ev ||event;
                let oLeft = add_box.offsetLeft;
                let oTop = add_box.offsetTop;
                let a =ev.clientX-oLeft;
                let b =ev.clientY-oTop;
                add_box.onmousemove =function (ev){
                    ev = ev || event;
                    add_box.style.left =ev.clientX-a+'px';
                    add_box.style.top =ev.clientY-b+'px';
                    $(add_box).css({'cursor':'default'})
                }
            }
            
        }
        edit_box.onmousedown = function(ev){
            if($(ev.toElement).attr('id')=='edit_people'){
                ev =ev ||event;
                let oLeft = edit_box.offsetLeft;
                let oTop = edit_box.offsetTop;
                let a =ev.clientX-oLeft;
                let b =ev.clientY-oTop;
                edit_box.onmousemove =function (ev){
                    ev = ev || event;
                    edit_box.style.left =ev.clientX-a+'px';
                    edit_box.style.top =ev.clientY-b+'px';
                    $(edit_box).css({'cursor':'default'})
                }
            }
        
        }
        document.onmouseup =function(){
            add_box.onmousemove =function(){
                null;
            }
            edit_box.onmousemove =function(){
                null;
            }
        }

})